/*
 * Created on 2006-10-27
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.coolbi.common.web.action;

/**
 * @author Clark
 * 
 *         TODO To change the template for this generated type comment go to
 *         Window - Preferences - Java - Code Style - Code Templates
 */
public class Pager {
	// 页面大小,即每页显示记录数
	private int pageSize = 20;

	// 组大小，即每组的页面连接数
	private int groupSize = 0;

	// 当前页号
	private int currPageNo = 1;

	// 总页数
	private int totalPageCount = 1;

	// 总组数
	private int totalGroupCount = 1;

	// 记录总数
	private int recordCount = 0;

	/**
	 * @return Returns the groupSize.
	 */
	public int getGroupSize() {
		return groupSize;
	}

	/**
	 * @param groupSize
	 *            The groupSize to set.
	 */
	public void setGroupSize(int groupSize) {
		if (groupSize > 0) {
			this.groupSize = groupSize;
		}
	}

	/**
	 * @return Returns the pageSize.
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize
	 *            The pageSize to set.
	 */
	public void setPageSize(int pageSize) {
		if (pageSize > 0) {
			this.pageSize = pageSize;
		}
	}

	/**
	 * @return Returns the recordCount.
	 */
	public int getRecordCount() {
		return recordCount;
	}

	/**
	 * @param recordCount
	 *            The recordCount to set.
	 */
	public void setRecordCount(int recordCount) {
		if (recordCount >= 0) {
			this.recordCount = recordCount;
			setTotalPageCountByRs();
			if (this.currPageNo > this.totalPageCount) {
				this.currPageNo = this.totalPageCount;
			}
			setTotalGroupCountByRs();
		}
	}

	/**
	 * @return Returns the currentPageNo.
	 */
	public int getCurrPageNo() {
		return currPageNo;
	}

	/**
	 * @param currentPageNo
	 *            The currentPageNo to set.
	 */
	public void setCurrPageNo(int currPageNo) {
		if (currPageNo > 0) {
			this.currPageNo = currPageNo;
		}
	}

	public int getTotalPageCount() {
		return this.totalPageCount;
	}

	public int getTotalGroupCount() {
		return this.totalGroupCount;
	}

	private void setTotalPageCountByRs() {
		if (this.pageSize > 0 && this.recordCount > 0
				&& this.recordCount % this.pageSize == 0) {
			this.totalPageCount = this.recordCount / this.pageSize;
		} else if (this.pageSize > 0 && this.recordCount > 0
				&& this.recordCount % this.pageSize > 0) {
			this.totalPageCount = (this.recordCount / this.pageSize) + 1;
		} else {
			this.totalPageCount = 1;
		}
	}

	private void setTotalGroupCountByRs() {
		if (this.groupSize == 0) {
			// 不分组
			totalGroupCount = 1;
		} else {
			// 分组
			totalGroupCount = (totalPageCount % groupSize) > 0 ? (totalPageCount
					/ groupSize + 1)
					: (totalPageCount / groupSize);
		}
	}
}
